package com.xuchen.zhenyang.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.pagination.PageHelper;
import com.xuchen.zhenyang.base.BaseQuery;
import com.xuchen.zhenyang.base.Result;
import com.xuchen.zhenyang.controller.base.BaseController;
import com.xuchen.zhenyang.core.annotation.RequestLog;
import com.xuchen.zhenyang.entity.CustomerImg;
import com.xuchen.zhenyang.entity.base.MyEntityWrapper;
import com.xuchen.zhenyang.service.CustomerImgService;
import com.xuchen.zhenyang.util.MyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 自定义图片 前端控制器
 * </p>
 *
 * @author xuchen
 * @since 2019-10-28
 */
@Controller
@RequestMapping("/customerImg")
public class CustomerImgController extends BaseController {

    @Autowired
    CustomerImgService imgService;
    @Value("${imgDir:imgDir}")
    String imgDir;

    @GetMapping("")
    String index(HttpServletRequest request) {
        setAttributeEnums(request);
        return "customer/img/customer-img-list";
    }

    @GetMapping("list")
    @ResponseBody
    Result list(BaseQuery baseQuery, CustomerImg myEntity, String params, HttpServletRequest request) {
        if (StrUtil.isNotBlank(params)) {
            myEntity = JSONObject.parseObject(params).toJavaObject(CustomerImg.class);
        }
        MyEntityWrapper<CustomerImg> wrapper = new MyEntityWrapper<>(baseQuery, myEntity);
        wrapper.like("name");
        List<CustomerImg> list = imgService.selectList(wrapper);
        list.forEach(item -> item.setRealAddress(null));
        return Result.success(PageHelper.freeTotal(), list);
    }

    @GetMapping("toAdd")
    String toAdd(HttpServletRequest request) {
        setAttributeEnums(request);
        return "customer/img/customer-img-add";
    }

    @PostMapping("doAdd")
    @ResponseBody
    Result doAdd(CustomerImg myEntity, String imgFile) {
        myEntity.setCreateUser(getSessionUserName());
        myEntity.setCreateTime(new Date());
        imgService.insert(myEntity);
        if (StrUtil.isNotBlank(imgFile)) {
            File newFile = MyUtils.createFileFromStr(imgFile, imgPath + imgDir, myEntity.getId() + ".jpg");
            myEntity.setUrl(imgDomain + imgDir + myEntity.getId() + ".jpg");
            myEntity.setRealAddress(newFile.getAbsolutePath());
            imgService.updateById(myEntity);
        }
        return Result.success();
    }

    @GetMapping("toEdit")
    String toEdit(CustomerImg myEntity, HttpServletRequest request) {
        setAttributeEnums(request);
        request.setAttribute("myEntity", imgService.selectById(myEntity).setRealAddress(null));
        return "customer/img/customer-img-edit";
    }

    @PostMapping("doEdit")
    @ResponseBody
    Result doEdit(CustomerImg myEntity, String imgFile) {
        myEntity.setUpdateUser(getSessionUserName());
        myEntity.setUpdateTime(new Date());
        if (StrUtil.isNotBlank(imgFile)) {
            File newFile = MyUtils.createFileFromStr(imgFile, imgPath + imgDir, myEntity.getId() + ".jpg");
            myEntity.setUrl(imgDomain + imgDir + myEntity.getId() + ".jpg");
            myEntity.setRealAddress(newFile.getAbsolutePath());
        }
        imgService.updateById(myEntity);
        return Result.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @RequestLog
    Result deleteImg(Integer id) {
        CustomerImg CustomerImg = imgService.selectById(id);
        File file = new File(CustomerImg.getRealAddress());
        if (file.exists()) {
            file.delete();
        }
        imgService.deleteById(id);
        return Result.success();
    }

    private void setAttributeEnums(HttpServletRequest request) {
    }
}

